<!--
 * @description  : 证据列表
 * @descriptionDetail: 证据列表
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-07-09 18:43:02
-->
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<view class="nav-right-icon">
				<u-icon name="plus" color="#FFF" @tap="docAdd"></u-icon>
			</view>
		</theme-navbar>
		<!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		 <fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->
		<view class="content">
			<me-table
			 :i="0"
			  :index="0"
			   :load-data="loadData"
			    :init-load="false"
			    ref="mescrollItem0"
			    >
				<view class="main" slot-scope="d">
					<u-row>
						<u-col :span="10">
							<view class="topLine">
								<text class="nameText">
									文件:{{ d.data.fileList[0].length }}份
								</text>
							</view>
							<view class="nameLine">
								证据类型: {{ d.data.evidTypeName }}
							</view>
							<view class="nameLine">
								图片类型: {{ d.data.explainItemName }}
							</view>
							<view class="nameLine">
								证据来源: {{ d.data.evidenceSourceName }}
							</view>
							<view class="nameLine">
								提取方法: {{ d.data.extractMethodName }}
							</view>
						</u-col>
						<u-col span="2" class="option-btn">
							<u-icon class="opt-item" name="trash" size="40" color="#FF0000" @tap="evidenceDelete(d.index)"></u-icon>
							<u-icon name="edit-pen" size="40" color="#2A79FF" @tap="evidenceEdit(d.data,d.index)"></u-icon>
						</u-col>
					</u-row>
					<u-line :color="$theme.u_border_color" />
				</view>
			</me-table>
			<view class="query-btn">
				<u-button type="primary" class="u-btn u-m-l-25 u-m-r-25" @tap="createDoc">
					生成文书
				</u-button>
				<u-button type="primary" class="u-btn" @tap="cancelDoc">
					取消
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import meTable from '@/components/me-table.vue';
	import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
	import caseService from '@/service/case/case.service.js';
	import fabButton from '@/components/uni-fab.vue';
	export default {
		components: {
			meTable,
			fabButton
		},
		mixins: [MescrollMoreMixin, ],
		data() {
			return {
				caseUuid: '',
				seleIndex: 'str', // 编辑选择的下表
				evidenceList: [],
				fContent: [{
					iconPath: require('@/static/add.png'),
					selectedIconPath: require('@/static/add.png'),
					text: '添加材料',
					active: false,
				}],
			};
		},
		onLoad() {
			let data = this.getRouteParam();
			this.caseUuid = data.caseUuid;
			let p = this.getPageIns(2);
			if (p && p.evidenceList) {
				this.evidenceList = p.evidenceList;
			}
		},
		watch: {
			evidenceList(value) {
				let p = this.getPageIns(2);
				if (p && p.evidenceList) {
					p.evidenceList = value;
				}
			},
		},
		created() {
			this.initData();
		},
		methods: {
			setRightBtns() {
				// 设置右侧标题
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("添加材料", () => {
					this.docAdd();
					this.setRightBtns();
				}, true, true);
				// #endif
			},
			fabtrigger(btn) {
				if (btn.index == 0) {
					this.docAdd()
				}
			},
			initData() {},
			docAdd() {
				this.seleIndex = 'str';
				this.setRouteParam('');
				this.$u.route('pages/case/caseInfoTemp/addEvidenceInfo');
			},
			// 删除证据
			evidenceDelete(index) {
				this.evidenceList.splice(index, 1)
			},
			// 编辑证据
			evidenceEdit(item, index) {
				this.seleIndex = index;
				this.setRouteParam(item);
				this.$u.route('pages/case/caseInfoTemp/addEvidenceInfo');
			},
			// 实地核查列表数据
			loadData(page) {
				return new Promise(resolve => {
					resolve({
						"data": this.evidenceList,
					});
				});
			},
			// 生成文书
			createDoc() {
				if (this.evidenceList.length == 0) {
					this.$u.toast('还没有上传证据');
				} else {
					let param = [];
					let psIndex = 0;
					this.evidenceList.forEach(itm => {
						if (itm.fileList && itm.fileList.length > 0) {
							itm.fileList.forEach(itp => {
								param.push({
									evidType: itm.evidType,
									explainItem: itm.explainItem,
									evidenceSource: itm.evidenceSource,
									extractMethod: itm.extractMethod,
									fileId: itp.gfiId,
									LAY_TABLE_INDEX: psIndex++,
									caseUuid: this.caseUuid,
								});
							});
						}
					});
					this.$modal.showLoading("正在生成文书...");
					caseService.addMoreDocumnet(param).then(res => {
						this.$modal.hideLoading();
						if (res.success) {
							this.$u.toast('生成文书成功');
							this.evidenceList = [];
							this.backPrev({
								paramKey: "evidenceInfo",
							}, 2);
						} else {
							this.$u.toast(res.msg);
						}
					}).catch(e => {
						this.$modal.hideLoading();
					})
				}
			},
			// 取消
			cancelDoc() {
				this.backPrev({
					paramKey: "evidenceInfo",
				});
			},
			// 文书添加返回操作,文书添加操作未完成
			updateBackState(param) {
				if (param.formItem) {
					if (typeof this.seleIndex == 'number') {
						this.evidenceList.splice(this.seleIndex, 1, param.formItem.obj)
					} else {
						this.evidenceList.push(param.formItem.obj)
					}
					this.$refs["mescrollItem0"].refresh();
				}
			},
		},
	};
</script>
<style lang="scss" scoped>
	.nav-right-icon {
		margin-right: 30rpx;
		font-size: 15 rpx;
		color: #fff;
	}

	.query-btn {
		display: flex;
		position: fixed;
		width: 95%;
		z-index: 999;
		bottom: var(--window-bottom, 0);

		.u-btn {
			width: 100%;
		}
	}

	.option-btn {
		display: flex;
		flex-direction: column;
		border-left: 4rpx solid #F2F3F6;

		.opt-item {
			margin-bottom: 30rpx;
		}
	}

	.main {
		margin: 10rpx 10rpx;
		padding: 10rpx 30rpx;
	}

	.topLine {
		padding: 0 10rpx;
		display: inline-flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}

	.nameText {
		color: black;
	}

	.nameLine {
		padding: 10rpx 10rpx;
		color: $u-tips-color;
	}
</style>
